﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data.Common;

namespace Model.Managers
{
    public static class UserManager
    {
        #region private Fields
        #endregion

        public static IList<Users> GetAllUser()
        {
            var db = new AdressDBEntities();
            var userList = (from c in db.Users
                            select c).ToList();
            return userList;
        }

        public static bool CheckUserConnexion(string userName, string userPwd, out UserDataManager manager)
        {

                Users user = null;
                var db = new AdressDBEntities();
                var userlist = (from u in db.Users
                            where u.UserName == userName
                            select u).ToList();
                if (userlist.Count() != 0)
                {
                    user = userlist.First();
                    if (user.UserPwd == userPwd)
                    {
                        manager = new UserDataManager(user);
                        return true;
                    }
                    else
                    {
                        manager = null;
                        return false;
                    }
                }
                else
                {
                    manager = null;
                    return false;
                }
            
        }
        public static void CreateNewUser(string userName, string userPwd)
        {
            var db = new AdressDBEntities();
            var user = Users.CreateUsers(-1, userName, userPwd);
            db.Users.AddObject(user);
            db.SaveChanges();
        }

        

    }
}
